package day_2022_12_to_3.day;

import java.util.Arrays;

/**
 * @author haomin
 * @date 2022/12/14 21:42
 **/
public class Day12_14_Num1697 {
    class Solution {
        public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {
            int group[]=new int[n+5],m=queries.length,o=edgeList.length;
            for(int i=0;i<n;i++){group[i]=i;}
            for(int i=0;i<m;i++){queries[i]=new int[]{i,queries[i][0],queries[i][1],queries[i][2]};}
            Arrays.sort(queries,(a,b)->a[3]-b[3]);
            Arrays.sort(edgeList,(a, b)->a[2]-b[2]);
            boolean ans[]=new boolean[m];
            for(int i=0,j=0;i<m;i++){
                while(j<o&&edgeList[j][2]<queries[i][3]){
                    union(edgeList[j][0],edgeList[j][1],group);
                    j++;
                }
                ans[queries[i][0]]=find(group[queries[i][1]],group)==find(group[queries[i][2]],group);
            }
            return ans;
        }
        void union(int a,int b,int group[]){
            a=find(a,group);
            b=find(b,group);
            group[b]=a;
        }
        int find(int a,int group[]){
            if(a!=group[a]){group[a]=find(group[a],group);}
            return group[a];
        }
    }
}